home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / TSR / STAY50 / DEMO.PAS < prev    next >
Pascal/Delphi Source File  |  1988-11-28  |  20KB  |  498 lines

  1.  
  2. {$I direct.inc}
  3.   {──────────────────────────────────────────────────────────────────────}
  4.   {         Turbo Pascal Stay Resident Shell Demonstation                }
  5.   {               Copyright (C) 1988 Lane  Ferris                        }
  6.   {──────────────────────────────────────────────────────────────────────}
  7.   {    Send Suggestions and Bug reports to COMPUSERVE ID: 70357,2716     }
  8.   {    or write:  4268 26th St. SanFrancisco, Ca 94131                   }
  9.   {──────────────────────────────────────────────────────────────────────}
  10.  
  11.  uses
  12.       crt,dos,
  13.       macros,          { assorted inlines }
  14.       SR50,            { stayres kernel   }
  15.       SR50subs,        { stayres subs     }
  16.       SRmsgu  ,        { mailbox unit     }
  17.       FListu    ;      { file list unit   }
  18.  
  19.  
  20.   const
  21.    AltD       : word = $2000         ; { AltD int 16 keycode            }
  22.    AltL       : word = $2600         ; { AltL int 16 keycode            }
  23.   var
  24.    Attr       : byte    ;
  25.  
  26.   {────────────────────────────────────────────────────────────────}
  27.   {                           Clock                                }
  28.   {────────────────────────────────────────────────────────────────}
  29.   {         Displays digital clock in upper right of screen        }
  30.   {────────────────────────────────────────────────────────────────}
  31.  {$F+}Procedure Clock ; {$F-}
  32.  
  33.   var
  34.     SystemTimer  : longint absolute $40:$6c ;
  35.     Hours        : longint   ;
  36.     minutes,
  37.     seconds      : longint   ;
  38.     ticks        : longint   ;
  39.  
  40.     Hoursstr    : string[2]  ;
  41.     Minutesstr  : string[2]  ;
  42.     secondsstr  : string[2]  ;
  43.     ampm        : string[2]  ;
  44.     ClockStr    : string[11] ;
  45.     SaveWindow  : array[1..4] of byte ;
  46.  
  47.     SaveCurPos  : word       ;
  48.     BiosCurPos   : word absolute $40:$50 ; { BIOS cursor position page 1  }
  49.  
  50.   BEGIN
  51.    While true do begin                  { do forever }
  52.       ticks   := SystemTimer     ;
  53.       Hours   := ticks div 65543 ;      { 65543 ticks per hour  }
  54.       dec(ticks,Hours*65543)     ;
  55.       minutes := ticks div 1092  ;      { 1092 ticks per minute }
  56.       dec(ticks,minutes*1092)    ;
  57.       seconds := ticks div 18    ;      { 18.2 ticks per second }
  58.  (**                                    { account for .2 tick error }
  59.       seconds := seconds - (seconds div 20) ; { as 1 tick in 20 err }
  60.   **)
  61.       if seconds >59 then seconds := 59 ;
  62.       if Hours > 12 then begin
  63.         dec(Hours,12)   ;
  64.         ampm := 'pm'    ;
  65.         end
  66.       else ampm := 'am' ;
  67.  
  68.       str(Hours  :2,hoursstr   ) ;
  69.       str(Minutes:2,minutesstr ) ;
  70.       str(seconds:2,secondsstr ) ;
  71.                                                { force leading zeros }
  72.       Hoursstr[1]   := char(ord(hoursstr[1])   or ord('0')) ;
  73.       Minutesstr[1] := char(ord(Minutesstr[1]) or ord('0')) ;
  74.       Secondsstr[1] := char(ord(Secondsstr[1]) or ord('0')) ;
  75.  
  76.       ClockStr := Hoursstr+':'+Minutesstr+':'+secondsstr+ampm  ;
  77.       resource(reserve,_crt)     ;
  78.       move(Windmin,SaveWindow,4) ;
  79.       SaveCurPos := BiosCurPos   ;
  80.       window(68,1,79,2)          ; { a window resets cursor posn etc }
  81.       write( ClockStr)           ;
  82.       move(SaveWindow,Windmin,4) ;
  83.       BiosCurPos := SaveCurPos   ;
  84.       resource(rlse,_crt)        ;
  85.       Yield                      ;  { give up cpu control }
  86.  
  87.    end {while true }         ;
  88.   END; {Clock}
  89.  
  90.   {───────────────────────────────────────────────────────────────}
  91.   {                       ShowDir                                 }
  92.   {───────────────────────────────────────────────────────────────}
  93.   {           Yet another directory display routine               }
  94.   {───────────────────────────────────────────────────────────────}
  95.   const
  96.    maxentries = 78 ; {≈1024 bytes}
  97.   var
  98.    Filenames    : array[1..maxentries] of string[13] ;
  99.    OldWindowPtr : pointer ; { pointer to old window on heap }
  100.   const
  101.    DirContents  : pointer = nil ; { process window contents to restore }
  102.   {───────────────────────────────────────────────────────────────}
  103.   {                       DirPop                                  }
  104.   {───────────────────────────────────────────────────────────────}
  105.   {       popup/dn maintenance routine called from SR50           }
  106.   {     each time the hotkey is activated from the keyboard       }
  107.   {───────────────────────────────────────────────────────────────}
  108.    {$F+} Procedure DirPop(popupdn:boolean) ; {$F-}
  109.  
  110.     Begin
  111.       resource(reserve,_crt)     ;
  112.      case popupdn of
  113.        True : Begin { This is a popup }
  114.               SaveWindow(1,1,68,20,OldWindowPtr) ; { save forgound window    }
  115.               BorderWindow(1,1,68,20,border) ;     { make window with border }
  116.               if DirContents <> nil then             { restore contents if any }
  117.                  RestoreWindow(2,2,67,19,DirContents) ;
  118.               end {popup} ;
  119.        false: Begin { this is a popdown}
  120.               SaveWindow(2,2,67,19,DirContents) ;       { save window contents }
  121.               RestoreWindow(1,1,68,20,OldWindowPtr) ; { restore foreground   }
  122.               end {popdown}
  123.      end {case};;
  124.       resource(rlse,_crt)     ;
  125.    End {DirPop} ;
  126.   {───────────────────────────────────────────────────────────────}
  127.   {                       Sort em                                 }
  128.   {───────────────────────────────────────────────────────────────}
  129.   {          Insertion sort filenames into alpa order             }
  130.   {───────────────────────────────────────────────────────────────}
  131.   Procedure Sortem(entries : integer ) ;
  132.    var
  133.    i, j, lowest, highest, center : integer ;
  134.    tempstr : string[13]                ;
  135.  
  136.   begin
  137.    for i := 2 to entries do begin
  138.      tempstr := Filenames[i] ;
  139.      lowest  := 1            ;
  140.      highest := i - 1        ;
  141.  
  142.      while lowest <= highest do begin
  143.       center := (lowest + highest) div 2 ;
  144.       if tempstr < filenames[center] then
  145.          highest := center - 1
  146.          else lowest := center +1   ;
  147.      end {while lowest..}   ;
  148.  
  149.      for j := i - 1 downto lowest do
  150.          filenames[j+1] := filenames[j] ;
  151.      filenames[lowest] := tempstr       ;
  152.    end {for i..}            ;
  153.   end {Sortem}        ;
  154.  
  155.   {───────────────────────────────────────────────────────────────}
  156.   {                       Show em                                 }
  157.   {───────────────────────────────────────────────────────────────}
  158.   {       display partial sorted directory entries on video       }
  159.   {───────────────────────────────────────────────────────────────}
  160.   Procedure Showem(entries : integer ) ;
  161.    var
  162.    i, j : integer ;
  163.   begin
  164.     clrscr  ;
  165.     j  := 0 ;
  166.     for i := 1 to entries do begin
  167.       Resource(reserve,_CRT) ;
  168.       write(filenames[i])    ;
  169.       Resource( rlse,_CRT)   ;
  170.       inc(j)                 ;
  171.       if j = 5 then begin
  172.         Resource(reserve,_CRT)  ;
  173.         writeln                 ;
  174.         Resource(rlse,_CRT)     ;
  175.         j := 0                  ;
  176.       end{if j}
  177.     end {for i}                   ;
  178.   end{showem} ;
  179.  
  180.   {───────────────────────────────────────────────────────────────}
  181.   {                  ShowDir         (main procedure)             }
  182.   {───────────────────────────────────────────────────────────────}
  183.   Procedure ShowDir ;
  184.    const
  185.     blanks : string[13] = '             ' ;
  186.    var
  187.     FilePath      : string    ;
  188.     FileAttr      : byte      ;
  189.     FileSearchRec : SearchRec ;
  190.     i             : integer   ;
  191.     ch            : char      ;
  192.  
  193.    begin {ShowDir}
  194.     FilePath  := '*.*'   ;
  195.     FileAttr  := AnyFile ;
  196.     i         := 1       ;
  197.  
  198.     FindFirst(FilePath,FileAttr,FileSearchRec) ;
  199.  
  200.  
  201.     while DosError = 0 do begin
  202.       With FileSearchRec do begin
  203.         blanks[0]    := char(13-length(name)) ;
  204.         Filenames[i] := Name+blanks           ;
  205.         inc(i)                         ;
  206.         if i = maxentries+1 then begin
  207.            sortem(i-1)                 ;
  208.            showem(i-1)                 ;
  209.            Resource(reserve,_CRT)      ;
  210.            writeln;write('Count was: ',i-1) ;
  211.            Resource(rlse,_CRT)         ;
  212.            while not keypressed do Yield    ;
  213.            ch := readkey               ; { eat the key       }
  214.            i  := 1                     ; { restart the array }
  215.         end {if i..}                   ;
  216.       end {with file..}                ;
  217.       FindNext( FileSearchRec )   ;
  218.    end{while DosError..}          ;
  219.  
  220.    sortem(i-1)                    ;
  221.    showem(i-1)                    ;
  222.    Resource(reserve,_CRT)         ;
  223.    writeln;writeln('Count was: ',i-1) ;
  224.    Resource(rlse,_CRT)                ;
  225.  
  226.    while not keypressed do yield      ;
  227.    ch := readkey                      ;
  228.  
  229.    End {ShowDir}                      ;
  230.  
  231.   {────────────────────────────────────────────────────────────────}
  232.   {                         DirTask                                }
  233.   {────────────────────────────────────────────────────────────────}
  234.   {    Hotkey task in infinite loop with Yield to SR50 at bottom   }
  235.   {────────────────────────────────────────────────────────────────}
  236.   Procedure DirTask              ;
  237.    begin
  238.    While true do begin
  239.     ShowDir                      ; { Display the Directory  }
  240.     Yield                        ; { tell SR50 its finished }
  241.    end {while true..}            ;
  242.   end {DirTask}                  ;
  243.   {────────────────────────────────────────────────────────────────}
  244.   {                         ListFile                               }
  245.   {────────────────────────────────────────────────────────────────}
  246.   {  If you're one who believes that Dinasours died of their own   }
  247.   {  stupditiy.. you'll love this.                                 }
  248.   {────────────────────────────────────────────────────────────────}
  249.   { This is an exercise in mailbox maintenance. It sends commands  }
  250.   { to a mailbox, and receives the results. Message passing is fun }
  251.   { .. but, ever so slow..  Dinasaurs dont care .                  }
  252.   {────────────────────────────────────────────────────────────────}
  253.   Const
  254.    ListContents : pointer = nil    ;  { contents of window }
  255.   {───────────────────────────────────────────────────────────────}
  256.   {                       ListPop                                 }
  257.   {───────────────────────────────────────────────────────────────}
  258.   {       popup/down maintenance routine called from SR50         }
  259.   {───────────────────────────────────────────────────────────────}
  260.    {$F+} Procedure ListPop(popupdn:boolean) ; {$F-}
  261.  
  262.     Begin
  263.       resource(reserve,_crt)     ;
  264.      case popupdn of
  265.        True : Begin { This is a popup }
  266.               SaveWindow(4,4,68,21,OldWindowPtr) ; { save forgound window    }
  267.               BorderWindow(4,4,68,21,border) ;     { make window with border }
  268.               if ListContents <> nil then          { restore contents if any }
  269.                  RestoreWindow(5,5,67,20,ListContents) ;
  270.               end {popup} ;
  271.        false: Begin { this is a popdown}
  272.               SaveWindow(5,5,67,20,ListContents) ;    { save window contents }
  273.               RestoreWindow(4,4,68,21,OldWindowPtr) ; { restore foreground   }
  274.               end {popdown}
  275.      end {case};;
  276.       resource(rlse,_crt)     ;
  277.    End {ListPop} ;
  278.   {───────────────────────────────────────────────────────────────}
  279.   {                       ListTask                                }
  280.   {───────────────────────────────────────────────────────────────}
  281.   {       Alt-L popup Showing lines of a file in window           }
  282.   {───────────────────────────────────────────────────────────────}
  283.    Procedure ListTask ;
  284.  
  285.      const
  286.        esc     =  27 ;
  287.        pgup    =  73 + 128 ;
  288.        pgdn    =  81 + 128 ;
  289.        uparr   =  72 + 128 ;
  290.        dnarr   =  80 + 128 ;
  291.        ctlpgup = 132 + 128 ;
  292.        ctlpgdn = 118 + 128 ;
  293.        ctlhome = 119 + 128 ;
  294.        ctlend  = 117 + 128 ;
  295.  
  296.        pagesize = 10 ;
  297.  
  298.      var
  299.        i          : integer ;
  300.        key        : integer ;   { keyboard input + 128 }
  301.        LineNr     : integer ;   { File line number     }
  302.        LastLineNr : integer ;   { Last line in file    }
  303.        Nrtoshow   : integer ;   { Num lines to show    }
  304.        result     : integer ;   { perverbial round can }
  305.        StrPtr     : pointer ;   { utility pointer      }
  306.        message    : string  ;   { utility string       }
  307.        done       : boolean ;   { utility boolean      }
  308.        textwidth  : byte    ;   { max text to write    }
  309.  
  310.     begin {main}
  311.  
  312.      MakeMailbox('ListMail')           ;  { Make a listing mailbox }
  313.  
  314.      While True do Begin       { repeat forever }
  315.       textwidth := lo(windmax) - lo(windmin) - 6 ;
  316.       Clrscr ;
  317.  
  318.       REPEAT {until done }
  319.         resource(reserve,_CRT)       ;
  320.         write('Enter Filename to List:');
  321.         resource(rlse,_CRT)          ;
  322.         Readln(Message)              ;
  323.         Message := 'Open '+Message   ;  { create Open file command  }
  324.         Send('ListMail',@Message)    ;  { Send command to mailbox   }
  325.         Receive('ListMail',strptr)   ;  { wait for message reply    }
  326.         if integer(strptr^) = 0
  327.          then done := true
  328.          else done := false ;
  329.       UNTIL  done = true    ;
  330.       LineNr := 1            ;
  331.       LastLineNr := maxint   ;
  332.       NrtoShow   := pagesize ;
  333.       resource(reserve,_CRT) ;
  334.       clrscr                 ;
  335.       gotoxy((lo(windmax)-lo(windmin))shr 1-7,
  336.              (hi(windmax)-hi(windmin))shr 1) ;
  337.       writeln( '<pgup><pgdn><'#24#25'>') ;
  338.       gotoxy(1,1)                        ;
  339.       resource(rlse,_CRT) ;
  340.  
  341.       REPEAT
  342.         key := byte(readkey) ;
  343.         if key = 0 then key := 128 + byte(readkey) ;
  344.         case key of
  345.  
  346.          uparr   : begin
  347.                    dec(LineNr,1)        ;
  348.                    Nrtoshow := 1        ;
  349.                    end                  ;
  350.          dnarr   : begin
  351.                    inc(LineNr)          ;
  352.                    Nrtoshow := 1        ;
  353.                    end                  ;
  354.          pgup    : begin
  355.                    dec(LineNr,pagesize) ;
  356.                    Nrtoshow := pagesize ;
  357.                    end                  ;
  358.          pgdn    : begin
  359.                    inc(LineNr,pagesize) ;
  360.                    NrtoShow := pagesize ;
  361.                    end                  ;
  362.          ctlPgup,
  363.          ctlHome : begin
  364.                    LineNr := 1          ;
  365.                    Nrtoshow := 1        ;
  366.                    end                  ;
  367.          ctlpgdn,
  368.          ctlEnd  : begin
  369.                    LineNr := maxint     ;
  370.                    Nrtoshow := 1        ;
  371.                    end                  ;
  372.          esc     :                      ;
  373.          else      key := 0             ;
  374.        end {case}                       ;
  375.  
  376.        if key <> 0 then begin
  377.          if LineNr > LastLineNr then LineNr := LastLineNr - 1;
  378.          if LineNr < 1 then LineNr := 1 ;
  379.          if LineNr-1+Nrtoshow > LastLineNr then
  380.             Nrtoshow := LastLineNr-LineNr+1 ;
  381.          for i := LineNr to LineNr-1+Nrtoshow do
  382.            begin
  383.            str(i,Message) ;
  384.            Message := 'Read '+Message      ;
  385.            Strptr  := @Message             ;
  386.            Send('ListMail',Strptr)         ; { Send readfile to mailbox }
  387.            Receive('ListMail',strptr)      ; { wait for message reply   }
  388.                                              { Strptr := FLgetNr(i) ;   }
  389.            if Strptr <> nil then begin
  390.             if string(Strptr^)[1] = #26 then
  391.                val(copy(string(Strptr^),2,5),LastLineNr,result) ;
  392.             if byte(Strptr^) > textwidth     { truncate string &  write }
  393.                then byte(Strptr^) := textwidth ;
  394.             if string(strptr^)[length(string(strptr^))-1] = ^M
  395.               then dec(string(strptr^)[0],2)     ;
  396.             resource(reserve,_crt)       ;
  397.             writeln(i:3,string(Strptr^)) ;
  398.             resource(rlse,_crt)          ;
  399.             end                          ;
  400.  
  401.            if (Strptr = nil) then            { an error has occured     }
  402.               LastLineNr := 1 ;
  403.           end {for..}                  ;
  404.        end {if key..}                  ;
  405.      UNTIL key = esc ;
  406.                                          { FLclose('test.dat') ;}
  407.       Message := 'Close sr50.pas'  ;
  408.       Send('ListMail',@Message)    ;  { Send open file to mailbox }
  409.       Receive('ListMail',strptr)   ;  { wait for message reply   }
  410.  
  411.      End {while True} ;
  412.     End {ListTask} ;
  413.   {────────────────────────────────────────────────────────────────}
  414.   {                  List Send/Receive task                        }
  415.   {────────────────────────────────────────────────────────────────}
  416.   {   Execute commands from 'ListMail' box and send back results   }
  417.   {────────────────────────────────────────────────────────────────}
  418.    Procedure ListCmds ;
  419.     var
  420.      Strptr : pointer   ;
  421.      result : integer   ;
  422.      lineNr : word      ;
  423.      Cmdstr : string[5] ;
  424.  
  425.     Begin
  426.  
  427.     While true do begin {forever}
  428.  
  429.  
  430.      REPEAT
  431.        { loop until Mailbox is created and a message is waiting }
  432.        Receive('ListMail',Strptr) ;
  433.        if Strptr = nil then yield      ;
  434.      UNTIL Strptr <> nil               ;
  435.  
  436.  
  437.      Cmdstr := copy(string(Strptr^),1,pos(' ',string(Strptr^))-1) ;
  438.      Caps(Cmdstr) ;
  439.  
  440.      If Cmdstr = 'OPEN'  then begin
  441.        result := FLopen(copy(string(Strptr^),6,sizeof(Filenamestr)-1)) ;
  442.        Send('ListMail',@result) ;
  443.        end {if..open} ;
  444.  
  445.      If Cmdstr = 'CLOSE' then begin
  446.        FLclose(copy(string(Strptr^),7,sizeof(Filenamestr)-1)) ;
  447.        result := 0                   ;
  448.        Send('ListMail',@result) ;
  449.        end {if..close} ;
  450.  
  451.      If CmdStr = 'READ' then begin
  452.  {$R-} val(copy(string(Strptr^),6,5),lineNr,result) ; {$R+}
  453.        if result <>0 then Strptr := nil
  454.          else  FLgetNr(lineNr,string(Strptr^)) ;   { get data string or  }
  455.        Send('ListMail',Strptr)        ;   { nil if end of file  }
  456.        end {if..read}                 ;
  457.  
  458.     end {while..forever} ;
  459.    End {ListSR} ;
  460.   {────────────────────────────────────────────────────────────────}
  461.   {                         Main                                   }
  462.   {────────────────────────────────────────────────────────────────}
  463.   begin {main}
  464.  
  465.     { Debug should be false to allow SR to go resident   }
  466.     { else it runs as a normal (if that's the word) task }
  467.  
  468.     SR50.Debug := false ;  { turn off/on debugging }
  469.     if paramstr(1) = 'debug' then SR50.Debug := true ;
  470.  
  471.     writeln ;
  472.     writeln(RUTidBlk.RUTidStr, ' is active'    ) ;
  473.     writeln;
  474.     writeln( '<AltD> toggles a directory list' ) ;
  475.     writeln( '<AltL> toggles a program list'   ) ;
  476.     writeln;
  477.     writeln('"DEMO quit" will terminate the demonstation')        ;
  478.     writeln;
  479.     writeln( ' copyright (c) 1988 Lane Ferris '       )        ;
  480.     writeln( '      The Hunters'' Helper'             )        ;
  481.     writeln ;
  482.  
  483.     Attr := textattr or $08 ;               ; { bright clock color    }
  484.  
  485.     Attach(@Clock,TimerType,18,NIL,'CLOCK') ; { Add Clock as a task   }
  486.  
  487.     Attach(@DirTask,KeyType,AltD,             { Add ShowDir task      }
  488.                         @DirPop,'DIRPOP')   ;
  489.     Attach(@ListTask,KeyType,AltL,            { Add List Display task }
  490.                       @ListPop,'LISTPOP')   ;
  491.     Attach(@ListCmds,TimerType,1,             { Add File Read task    }
  492.                           NIL,'LISTCMDS')   ;
  493.     StartTSR                                ; { jump to TSR code      }
  494.                                               { never to return here  }
  495.   end.  {main}
  496.  
  497.       (**)FREEZE;NMI;(**)
  498.